<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class CostsetController extends AdminBaseController
{
    public function initialize()
    {
        $this->deal_model = Db::name("costset");
        parent::initialize();
    }


    /*费用设定编辑--跳转传值*/
    public function edit()
    {

        $result = Db::name("Costset")
            ->where('id', 1)
            ->find();
        if(empty($result)){
            $result['tomb_cost'] = 0;
            $result['deposit_cost'] = 0;
            $result['manager_cost'] = 0;
            $result['move_cost'] = 0;
            $result['id_cost'] = 0;
            $result['id'] = 0;
        }

        $this->assign($result);
        return $this->fetch();
    }

    /*附件编辑--保存*/
    public function edit_post()
    {
        $data = $this->request->param();

        if ($data['tomb_cost'] < 0){
            $this->error('安葬费的金额不可为负数');
        }
        if ($data['deposit_cost'] < 0){
            $this->error('寄存费的金额不可为负数');
        }
        if ($data['move_cost'] < 0){
            $this->error('迁墓费的金额不可为负数');
        }
        if ($data['manager_cost'] < 0){
            $this->error('维护费的金额不可为负数');
        }
        if ($data['id_cost'] < 0){
            $this->error('工本费的金额不可为负数');
        }

        if($data['id'] == 0){
            $data['add_time'] = time();
            $data['add_user'] = cmf_get_current_admin_id();
            Db::name("costset")->insertGetId($data);
        }else{
            Db::name("costset")->update($data);
        }
        $this->success("保存成功", url("Costset/edit"));
    }

}